﻿/* *****************************************************************************
ShoutcastRunner 0.3

Copyright (c) KEXP 90.3 FM Seattle. All rights reserved. 
www.kexp.org
author: Louis O'Callaghan (louis@kexp.org)
October 2012

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0  

THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
MERCHANTABLITY OR NON-INFRINGEMENT. 

See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.

SHOUTcast™ is a trademark of AOL LLC.
***************************************************************************** */

using System;
using System.Configuration;
using System.IO;
using System.Reflection;

namespace Kexp.ShoutcastRunner
{
    public static class Settings
    {
        private static string _shoutcastDir;

        public const int DefaultRotationDay = 1;
        public const int DefaultOverwriteLogSizeLimit = 50000; // 50KB
        public const int DefaultOverwriteW3CSizeLimit = 300000; // 300KB

        public static bool EnableContentMonitoring
        {
            get
            {
                bool? enabled = Helpers.GetAppSettingAsBool("EnableContentMonitoring");
                return enabled != null && enabled.Value;
            }
        }

        public static bool EnableCrossdomainServer
        {
            get 
            { 
                bool? enabled = Helpers.GetAppSettingAsBool("EnableCrossdomainServer");
                return enabled != null && enabled.Value;
            }
        }

        #region Shoutcast locations

        /// <summary>
        /// Returns the value specified in the App.Config, otherwise checks for and returns a default location
        /// </summary>
        /// <returns></returns>
        public static string ShoutcastContentDirectory
        {
            get
            {
                string shoutcastContent = ConfigurationManager.AppSettings["ShoutcastContentDirectory"];
                if (!shoutcastContent.IsNullOrEmptyOrWhiteSpace() && new DirectoryInfo(shoutcastContent).Exists)
                    return shoutcastContent;

                var shoutcastContentInfo = new DirectoryInfo(Path.Combine(ShoutcastDirectory, "content"));
                if (!shoutcastContentInfo.Exists)
                    throw new ConfigurationErrorsException("Can't find Shoutcast content directory on this computer.");

                return shoutcastContentInfo.FullName;
            }
        }

        public static string ShoutcastDirectory
        {
            get
            {
                if (!_shoutcastDir.IsNullOrEmptyOrWhiteSpace())
                    return _shoutcastDir;

                var shoutcastExe = new FileInfo(ShoutcastExecutable);
                _shoutcastDir = shoutcastExe.DirectoryName;
                return _shoutcastDir;
            }
        }

        /// <summary>
        /// Returns the value specified in the App.Config, otherwise checks for and returns a default location
        /// </summary>
        /// <returns></returns>
        public static string ShoutcastExecutable
        {
            get
            {
                string shoutcastExe = ConfigurationManager.AppSettings["ShoutcastExe"];
                if (!shoutcastExe.IsNullOrEmptyOrWhiteSpace() && new FileInfo(shoutcastExe).Exists)
                    return shoutcastExe;

                string programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
                if (programFiles.IsNullOrEmptyOrWhiteSpace())
                    throw new ConfigurationErrorsException("Can't find Program Files directory on this computer.");

                var shoutcastInfo = new FileInfo(Path.Combine(programFiles, @"shoutcast\sc_serv.exe"));
                if (!shoutcastInfo.Exists)
                    throw new ConfigurationErrorsException("Can't find Shoutcast on this computer.");

                return shoutcastInfo.FullName;
            }
        }

        public static string ShoutcastExecutableDirectory
        {
            get
            {
                try
                {
                    return new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
                }
                catch
                {
                    return null;
                }
            }
        }

        #endregion

        #region Log settings

        public static int DayOfMonthlyLogRotation
        {
            get
            {
                int? day = Helpers.GetAppSettingAsInt("DayOfMonthlyLogRotation");
                return day == null ? DefaultRotationDay : day.Value;
            }
        }

        /// <summary>
        /// Log files smaller than this will be overwritten. The idea here is that the entire log is 
        /// parsed every day, and we don't want to write out files that have already been written. So 
        /// files larger than this size will be kept. A better way to do this would probably be to 
        /// analyze the actual dates in the log file but I was in a hurry
        /// </summary>
        public static int OverwriteLogSizeLimit
        {
            get
            {
                int? size = Helpers.GetAppSettingAsInt("OverwriteLogSizeLimit");
                return size == null ? DefaultOverwriteLogSizeLimit : size.Value;
            }
        }

        public static bool ParseDailyLogs
        {
            get
            {
                bool? utc = Helpers.GetAppSettingAsBool("ParseDailyLogs");
                return utc != null && utc.Value;
            }
        }

        public static string ParseW3CDailyLogs
        {
            get
            {
                string config = ConfigurationManager.AppSettings["ParseW3CDailyLogs"];

                if (config != "triton" && config != "true")
                    return "false";

                return config;
            }
        }

        /// <summary>
        /// Log files smaller than this will be overwritten. The idea here is that the entire log is 
        /// parsed every day, and we don't want to write out files that have already been written. So 
        /// files larger than this size will be kept. A better way to do this would probably be to 
        /// analyze the actual dates in the log file but I was in a hurry
        /// </summary>
        public static int OverwriteW3CSizeLimit
        {
            get
            {
                int? size = Helpers.GetAppSettingAsInt("OverwriteW3CSizeLimit");
                return size == null ? DefaultOverwriteW3CSizeLimit : size.Value;
            }
        }

        #endregion
    }
}
